<template>
  <div>
    <div id="printHtml" v-html="printDataHtml" style="position: absolute;top: -9999px;left: -9999px;" ></div>
    <tiny-dialog-box destroy-on-close close-on-press-escape v-model:visible="showPrintPanel" title="打印小票" @close="hide" width="350px">
      <div v-if="showTitle" style="text-align: center;font-size: 22px;">装车成功</div>
      <div style="margin-top: 60px;text-align: center;">
        <j-button   @click="toPrint(1)" type="primary" style="width: 180px;height: 50px;">打印 小票</j-button>
      </div>
      <div style="height: 100px;"></div>
      <div style="margin-top: 50px;text-align: center;margin-bottom: 50px;" v-if="showPrintAdd">
        <JButton   @click="toPrint(4)"  style=" width: 180px;height: 50px;" ><i class="fa fa-plus"  /> 新增 装车</JButton >
      </div>
    </tiny-dialog-box>
  </div>
</template>

<script>
import tool from "@/utils/tool";
export default {
  name: "doprint",
  props: {
    showTitle: {type: Boolean, default: false},
    showPrintBill: {type: Boolean, default: true},
    showPrintBoth: {type: Boolean, default: true},
    showPrintAdd: {type: Boolean, default: false},
  },
  data () {
    return {
      printDataHtml: '',
      showPrintPanel: false, // 选择打印类型
      sendPrintName: '',
      salePrintName: '',
      printView: false,
      printDetail: [] // 需要打印明细的租户
    }
  },
  created () {
    this.getPrintName()
  },
  methods: {
    // 取打印机名称
    getPrintName () {
      tool.post('app/py',{which: 'getPsSets' }, false).then(ret => {
        if(ret && ret.data) {
          let sets = JSON.parse(ret.data.sets)
          if (!sets.sendPrintName) return tool.err('没有发现打印机')
          this.sendPrintName = sets.sendPrintName
        }
      })
    },
    // 打印
    print (item) {
      // 同时打印票据 和 送货单
      const LODOP = (typeof getCLodop === 'function') ? getCLodop() : ''
      if (LODOP) { 
        // tool.post('deliver/getPrint', {data: item,sendPrintName:this.sendPrintName}).then(ret => {
        tool.post('justdeliver/getPrint', {cargoId: item,sendPrintName:this.sendPrintName}).then(ret => {
          if(ret.stat) {
            this.printDataHtml = ret.data.data
            this.printView = ret.data.printView // 是否打印预览
            this.showPrintPanel = true
          }
        })
      }
      else {
        tool.err('没有打印驱动')
      }
    },
    // 打印选择
    toPrint (index) {
      if(index === 1) {
        if (!this.sendPrintName) return tool.err('没有设置小票打印机')
        this.sendPrint()
      }
      if (index === 4) {
        this.$emit('addNew')
        setTimeout(() => {
          this.showPrintPanel = false
        }, 50)
      }
    },
    // 打印小票
    sendPrint() {
      let dom = document.getElementById("printHtml");
      setTimeout(() => {
        let rects = dom.getClientRects()
        LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
        LODOP.PRINT_INIT("送货单小票打印");
        LODOP.SET_LICENSES("","C748D766DD08B5AE84E6BC2F3A546E6C7C9","","");
        LODOP.SET_PRINT_PAGESIZE(3, '80mm',45,"") //这里3表示纵向打印且纸高“按内容的高度”；1385表示纸宽138.5mm；45表示页底空白4.5mm
        LODOP.ADD_PRINT_TABLE(40, 0, 800, rects[0].height+160 , this.printDataHtml)
        if (LODOP.SET_PRINTER_INDEX(this.sendPrintName)) {
          if(this.printView) {
            LODOP.PREVIEW();
          }
          else {
            LODOP.PRINT();
          }
          this.showPrintPanel = false

        } else {
          tool.err('读取小票打印机失败')
        }
      }, 50)
    },
    hide () {
      this.$emit('hide')
    }
  },
}
</script>

<style scoped>

</style>

<!--<template>-->
<!--<div>-->
<!--  <div id="printHtml" v-html="state.printDataHtml" style="position: absolute;top: -9999px;left: -9999px;" ></div>-->
<!--  <tiny-dialog-box destroy-on-close close-on-press-escape v-model:visible="state.showPrintPanel" title="打印小票" @close="hide" width="350">-->
<!--    <div v-if="showTitle" style="text-align: center;font-size: 22px;">装车成功</div>-->
<!--    <div style="margin-top: 60px;text-align: center;">-->
<!--      <tiny-button  :loading="state.loading" @click="toPrint(1)" type="info" style="width: 180px;height: 50px;">打印 小票</tiny-button>-->
<!--    </div>-->
<!--    <div style="height: 100px;"></div>-->
<!--    <div style="margin-top: 50px;text-align: center;margin-bottom: 50px;" v-if="showPrintAdd">-->
<!--      <JButton   @click="toPrint(4)"  style="width: 180px;height: 50px;" ><i class="fa fa-plus"  /> 新增 装车</JButton >-->
<!--    </div>-->
<!--  </tiny-dialog-box>-->
<!--</div>-->
<!--</template>-->

<!--<script setup>-->
<!--import tool from "@/utils/tool";-->
<!--import {ref} from "vue";-->
<!--const state = ref({-->
<!--  printDataHtml: '',-->
<!--  showPrintPanel: false, // 选择打印类型-->
<!--  sendPrintName: '',-->
<!--  salePrintName: '',-->
<!--  printView: false,-->
<!--  loading: false,-->
<!--  cargoId: '',-->
<!--  printDetail: [] // 需要打印明细的租户-->
<!--})-->
<!--const emit = defineEmits(['hide', 'addNew'])-->
<!--const props = defineProps({-->
<!--  showTitle: {type: Boolean, default: false},-->
<!--  showPrintBill: {type: Boolean, default: true},-->
<!--  showPrintBoth: {type: Boolean, default: true},-->
<!--  showPrintAdd: {type: Boolean, default: false},-->
<!--})-->
<!--// 取打印机名称-->
<!--const getPrintName = () => {-->
<!--  tool.post('app/py',{which: 'getPsSets' }, false).then(ret => {-->
<!--    if(ret && ret.data) {-->
<!--      let sets = JSON.parse(ret.data.sets)-->
<!--      if (!sets.sendPrintName) return tool.err('没有发现打印机')-->
<!--      state.value.sendPrintName = sets.sendPrintName-->
<!--    }-->
<!--  })-->
<!--}-->
<!--// 打印-->
<!--const print = (cargoId) => {-->
<!--  getPrintName()-->
<!--  state.value.cargoId = cargoId-->
<!--  state.value.showPrintPanel = true-->

<!--}-->
<!--const toPrint = (index) => {-->
<!--  if(index === 1) {-->
<!--    if (!state.value.sendPrintName) return tool.err('没有设置小票打印机')-->
<!--    sendPrint()-->
<!--  }-->
<!--  if (index === 4) {-->
<!--    emit('addNew')-->
<!--    setTimeout(() => {-->
<!--      state.value.showPrintPanel = false-->
<!--    }, 50)-->
<!--  }-->
<!--}-->
<!--// 打印小票-->
<!--const sendPrint  = () => {-->
<!--  // 同时打印票据 和 送货单-->
<!--  const LODOP = (typeof getCLodop === 'function') ? getCLodop() : ''-->
<!--  if(!state.value.cargoId) return tool.err('没有数据')-->
<!--  if (LODOP) {-->
<!--    state.value.loading = true-->
<!--    setTimeout(() => {-->
<!--      state.value.loading = false-->
<!--    }, 3000)-->
<!--    tool.post('justdeliver/getPrint', {cargoId: state.value.cargoId,sendPrintName:state.value.sendPrintName}).then(ret => {-->
<!--      if(ret.stat) {-->
<!--        state.value.printDataHtml = ret.data.data-->
<!--        state.value.printView = ret.data.printView // 是否打印预览-->
<!--        let dom = document.getElementById("printHtml");-->
<!--        setTimeout(() => {-->
<!--          let rects = dom.getClientRects()-->
<!--          let barTop = tool.minus(rects[0].height, 120)-->
<!--          if (barTop < 300) barTop += 20-->
<!--          // LODOP.ADD_PRINT_BARCODE(barTop+'px', '90px', '100px', '100px', 'QRCode', '180001-' + item.id);-->
<!--          state.value.top = barTop-->
<!--          LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)-->
<!--          LODOP.PRINT_INIT("送货单小票打印");-->
<!--          LODOP.SET_LICENSES("","C748D766DD08B5AE84E6BC2F3A546E6C7C9","","");-->
<!--          LODOP.SET_PRINT_PAGESIZE(3, '80mm',45,"") //这里3表示纵向打印且纸高“按内容的高度”；1385表示纸宽138.5mm；45表示页底空白4.5mm-->
<!--          LODOP.ADD_PRINT_TABLE(40, 0, 800, rects[0].height, state.value.printDataHtml)-->
<!--          tool.suc('打印中.....')-->
<!--          LODOP.PREVIEW();-->
<!--          return-->
<!--          if (LODOP.SET_PRINTER_INDEX(state.value.sendPrintName)) {-->
<!--            if(state.value.printView) {-->
<!--              LODOP.PREVIEW();-->
<!--            }-->
<!--            else {-->
<!--              LODOP.PRINT();-->
<!--            }-->
<!--            state.value.showPrintPanel = false-->
<!--            // if (LODOP.CVERSION) {-->
<!--            //   CLODOP.On_Return = (TaskID,Value)=> {-->
<!--            //     if (Value !== 0 || Value !== false) {-->
<!--            //       state.value.loading = false-->
<!--            //       // tool.post('print/billPrintSuc', {cargoId: cargoId}).then(ret => {})-->
<!--            //     }-->
<!--            //   }-->
<!--            // }-->
<!--            state.value.loading = false-->
<!--          }-->
<!--          else {-->
<!--            state.value.loading = false-->
<!--            tool.err('读取小票打印机失败')-->
<!--          }-->
<!--        }, 50)-->
<!--      }-->
<!--    })-->
<!--  }-->
<!--  else {-->
<!--    tool.err('没有打印驱动')-->
<!--  }-->
<!--}-->
<!--const hide = () => {-->
<!--   emit('hide')-->
<!--}-->


<!--defineExpose({print})-->

<!--</script>-->

<!--<style scoped>-->

<!--</style>-->
